package com.wie.staffhubpro.controller;

import com.wie.staffhubpro.domain.dto.EmpDTO;
import com.wie.staffhubpro.domain.dto.Register1DTO;
import com.wie.staffhubpro.domain.dto.Register2DTO;
import com.wie.staffhubpro.domain.entity.Emp;
import com.wie.staffhubpro.domain.entity.Result;

import com.wie.staffhubpro.domain.vo.RegisterVO;
import com.wie.staffhubpro.service.EmpService;
import com.wie.staffhubpro.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private UserService userService;

    /**
     * 用户注册阶段一
     */
    @PostMapping("/register_1")
    public Result register1 (@RequestBody Register1DTO registerDTO) {
        log.info("注册阶段一: {}", registerDTO);
        RegisterVO registerVO = userService.registerPre(registerDTO);
        return Result.success(registerVO);
    }

    /**
     * 用户注册阶段二
     */
    @PostMapping("/register_2")
    public Result register2 (@RequestBody Register2DTO registerDTO) {
        log.info("注册阶段二: {}", registerDTO);
        userService.register(registerDTO);
        return Result.success();
    }

    /**
     * 获取员工列表
     */
    @GetMapping("/list")
    public Result getList () {
        log.info("获取员工列表");
        List<Emp> list = empService.listEmp();
        return Result.success(list);
    }

    /**
     * 根据部门id查询员工
     */
    @GetMapping("/getByDeptId")
    public Result getByDeptId (@RequestParam Integer deptId) {
        log.info("根据部门id查询员工: {}", deptId);
        List<Emp> empList = empService.getByDeptId(deptId);
        return Result.success(empList);
    }

    /**
     * 根据角色id查找员工
     */
    @GetMapping("/getByRoleId")
    public Result getByRoleId (@RequestParam String roleId) {
        log.info("根据角色id查找员工: {}", roleId);
        List<Emp> empList = empService.getByRoleId(roleId);
        return Result.success(empList);
    }

    /**
     * 根据员工查询员工
     */
    @GetMapping("/getByStatus")
    public Result getByStatus (@RequestParam String status) {
        log.info("根据员工状态查询员工: {}", status);
        List<Emp> empList = empService.getByStatus(status);
        return Result.success(empList);
    }

    /**
     * 根据员工姓名模糊查询员工
     */
    @GetMapping("/getByName")
    public Result getByName (@RequestParam String empName) {
        log.info("根据员工姓名模糊查询员工: {}", empName);
        List<Emp> empList =  empService.getByName(empName);
        return Result.success(empList);
    }

    /**
     * 根据员工 id 查询
     */
    @GetMapping("/getById")
    public Result getByEmpId (@RequestParam Integer empId) {
        log.info("通过empId查询: {}", empId);
        Emp emp = empService.getById(empId);
        return Result.success(emp);
    }

    /**
     * 通过deptId查找部长
     */
    @GetMapping("/getBoss")
    public Result getBossByDeptId (Integer deptId) {
        log.info("通过deptId查找部长: {}", deptId);
        List<Emp> empList = empService.getBossByDeptId(deptId);
        return Result.success(empList);
    }

    /**
     * 新增员工
     */
    @PostMapping("/add")
    public Result add(@RequestBody EmpDTO empDTO) {
        log.info("新增员工: {}", empDTO);
        empService.addEmp(empDTO);
        return Result.success();
    }

    /**
     * 根据id修改员工信息
     */
    @PostMapping("/alter")
    public Result alter(@RequestBody Emp emp) {
        log.info("更新员工信息, {}", emp);
        empService.alterEmp(emp);
        return Result.success();
    }

}
